package main;

import common.ExcelUtil;
import common.WordUtil;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {

    public static final String englishTableName = "英文表名称";
    public static final String tableEName = "中文表名称";
    public static final String field = "英文字段名";
    public static final String dateType = "数据类型";
    public static final String eField = "中文字段名";
    public static final String isKey = "是否主键";
    public static final String filedIsNull = "是否允许为空";

    private static void tables() {
        try {
            XWPFDocument xdoc = new XWPFDocument();
            HashMap<String, List<Map<String, String>>> hashMap = new HashMap<>();

            //获取数据
            /*
                -- mysql查询表名、表注释、表字段数据
                SELECT
                    t.table_name AS '表名称',
                    t.table_comment AS '表注释',
                    c.column_name AS '字段名称',
                    c.column_type AS '数据类型',
                    c.column_comment AS '字段注释',
                    c.column_key AS '是否主键',
                    c.is_nullable AS '是否允许NULL'
                FROM
                    information_schema.COLUMNS c
                    JOIN information_schema.TABLES t ON c.table_name = t.table_name
                WHERE
                    c.table_schema = (
                SELECT DATABASE
                    ());
             */
            File file = new File("C:\\Users\\Sunday\\Desktop\\dataTableName.xlsx");
            List<Map<String, String>> list = ExcelUtil.readExcel3(file, 0);

            //处理数据，调整成下面的格式
            /*
                [
                    {"表名称":[
                        {},//一条条字段信息
                        {},//一条条字段信息
                        {},//一条条字段信息
                    ]}
                ]
             */
            ArrayList<Map<String, String>> arrayList = new ArrayList<>();
            String tableName = "";
            for (int i = 0; i < list.size(); i++) {
                Map<String, String> map = list.get(i);
                String tName = String.valueOf(map.get("英文表名称"));
                if (tableName.equals(tName)) {
                    arrayList.add(map);
                } else {
                    hashMap.put(tableName, arrayList);
                    tableName = tName;
                    arrayList = new ArrayList<>();
                    arrayList.add(map);
                }

                if (list.size() - i == 1) {
                    hashMap.put(tableName, arrayList);
                }
            }


            int i = 1;
            //生成内容
            for (String tName : hashMap.keySet()) {
                if ("".equals(tName)) {
                    continue;
                }
                List<Map<String, String>> maps = hashMap.get(tName);
                String tZs = String.valueOf(maps.get(0).get("中文表名称"));

                //设置文字，对表格进行描述
                XWPFParagraph xp = xdoc.createParagraph();
                xp.setSpacingBefore(0);
                XWPFRun r1 = xp.createRun();
                r1.setFontFamily("宋体");
                r1.setFontSize(12);
                r1.setTextPosition(0);

//                r1.addBreak(); // 换行
//                r1.setText("表名称：" + tName);
                r1.addBreak(); // 换行
//                r1.setText("表注释：" + tZs);
                String format = String.format("表4-%s %s", i, tZs);
                r1.setText(format);
                r1.addBreak(); // 换行

                i++;
                //表格标题
                String[] titles = {
                        field,
                        eField,
                        dateType,
                        isKey,
                        filedIsNull
                };

                //表格内容
                List<Map<String, String>> values = new ArrayList<>();
                for (Map<String, String> stringStringMap : maps) {
                    String cZs = stringStringMap.get(field);
                    String isPri = stringStringMap.get(eField);
                    String cDateTye = stringStringMap.get(dateType);
                    String key = stringStringMap.get(isKey);
                    String strNull = stringStringMap.get(filedIsNull);

                    //按照表格标题格式进行封装
                    HashMap<String, String> stringStringHashMap = new HashMap<>();
                    stringStringHashMap.put(field, cZs);
                    stringStringHashMap.put(eField, isPri);
                    stringStringHashMap.put(dateType, cDateTye);
                    stringStringHashMap.put(isKey, key);
                    stringStringHashMap.put(filedIsNull, strNull);

                    values.add(stringStringHashMap);
                }


                WordUtil.createSimpleTable(xdoc, titles, values);
            }

            //保存word文件
            FileOutputStream fos = new FileOutputStream("E:\\Test1.doc");
            xdoc.write(fos);
            fos.close();

            System.out.println("操作完成！");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        tables();
    }
}